const app = getApp()
var userAPI=require('../../api/User')
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const defaultAvatarUrl = './../index/images/user/smile.svg'
const defaultnickname='微信用户'
Page({
  data: {
    nickname:defaultnickname,
    avatarUrl: defaultAvatarUrl,
    theme: wx.getSystemInfoSync().theme,
  },
  async onLoad() {
    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme
      })
    })
    
    await userAPI.getUserInfo().then((res)=>{
      // app.data.currentUser.iconUrl='嘿嘿'
     var avatarUrl=app.data.currentUser.iconUrl
      this.setData({avatarUrl:avatarUrl,nickname:app.data.currentUser.nickName})
      
    //   appJs.setData({nickname:res.data.result.nickname,                                           
    //                avatarUrl:res.data.result.avatar==null?defaultAvatarUrl:res.data.result.avatar}
    // )
  }
    )
  },
  onShow(){
  
  },
 async onChooseAvatar(e) {
    var _this=this
    const { avatarUrl } = e.detail 
    var nickname='微信用户'
    await userAPI.getUserInfo().then((res)=>{nickname=res.data.result.nickname})
    var cloudPath='icon/'+wx.getStorageSync('userId')+'.png'
    await wx.cloud.uploadFile({
      cloudPath: cloudPath,
      filePath :avatarUrl,
    }).then((res=>{
      console.log(res)
      userAPI.updateUser(nickname,res.fileID).then((res)=>{console.log(res)}).catch((error)=>{console.log(error)})
      _this.setData({avatarUrl:res.fileID})
    }))
    this.setData({
      avatarUrl,
    })
    
  },
 async  changeNickname(e){
    var nickName=e.detail.value
    var avatarUrl=this.data.avatarUrl
   userAPI.updateUser(nickName,avatarUrl).then((res)=>{console.log(res)})
 }
})
